package shell.command;

import java.io.PrintStream;
import java.util.Iterator;
import java.util.Set;


/**
 * Commande pour lister les variables.
 * 
 * @version 1.0
 */
public final class VarCommandImpl extends ShellCommandImpl {
    /**
     * Constructeur.
     * 
     * @param context
     *            Contexte d'exécution.
     */
    public VarCommandImpl(final ShellContext context) {
	super(context);
    }

    /**
     * @see shell.command.ShellCommand#getName().
     */
    public String getName() {
	return "var";
    }

    /**
     * @see shell.command.ShellCommand#getUsage().
     */
    public String getUsage() {
	return "var";
    }

    /**
     * @see shell.command.ShellCommand#getShortDescription().
     */
    public String getShortDescription() {
	return "List the variables";
    }

    /**
     * @see shell.command.ShellCommand#execute(String, PrintStream, PrintStream)
     *      .
     */
    public void execute(final String cmdline, final PrintStream out,
	    final PrintStream err) {
	try {
	    Set<String> varNames = super.getContexte().getVarNames();
	    Iterator<String> iterator = varNames.iterator();
	    if (iterator == null) {
		return;
	    }
	    StringBuffer sb = new StringBuffer();
	    while (iterator.hasNext()) {
		String key = (String) iterator.next();
		Object value = super.getContexte().getVar(key);
		sb.append(key);
		sb.append('=');
		if (value != null) {
		    sb.append(value.toString());
		}
		sb.append(System.getProperties().getProperty("line.separator"));
	    }
	    out.println(sb);

	} catch (final Exception e) {
	    e.printStackTrace(err);
	}
    }
}
